<?php
namespace app\admin\controller;


use cubox\Cubox;
use app\admin\library\User;
use app\admin\library\AdminController;
use cubox\File;

class Index extends AdminController{
    public function onIndex(){
        return $this->display();
    }

    public function onTest(){
       $db = Cubox::db('mssql');
       print_r($db ->query("EXEC sp_help @objname ='dbo.test'"));
       
    }

    public function onUserInit(){

        $appInfoList =  Cubox::getAppList();
        $appList = [];
        foreach ($appInfoList as $key => $app){
            
            $info  = [];
            if (User::power($key)){

                if (isset($app['admin']) &&  isset($app['admin']['controller'])){
                   $controller = $app['admin']['controller'];
                   $action =   $app['admin']['action']??'index';
                   $info['admin_url'] = Cubox::url($key,$controller,$action);
                }else{
                    $info['admin_url'] = '';
                }

                $info['name'] = $key;
                $info['title'] = $app['title'];
                $info['description'] = $app['description'];
                $info['icon'] = $this->assetUrl(ROOT.DS.APP.DS.$key.DS.$app['icon']);
                $info['version'] = $app['version'];
                $info['author'] = $app['author'];
                $appList []= $info;
            }
            
            
        }

        return ['appList'=>$appList];
    }


    public function onEditorInit(){
        $file =  'tinymce/tinymce.min.js';

        $webpackPath = RUN.DS.WEBPACK.DS.$file;

        if (!file_exists($webpackPath)){
            $resourcesFolder = ROOT.DS.APP.DS.'admin'.DS.'assets'.DS.'tinymce';

            if (!file_exists($resourcesFolder)){
                throw new \Exception('编辑器文件不存在');
            }
           //copy文件夹到webpack
            File::copyFolder($resourcesFolder,dirname($webpackPath));
        }

        return ['editorUrl'=>'/'.WEBPACK.'/'.$file];
    }
}